package com.example.kleinanzeigengenerator.kamera;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;

import com.example.kleinanzeigengenerator.R;
import com.example.kleinanzeigengenerator.common.Constants;

public class Kamera extends Activity {
	Camera camera;
	Vorschau vorschau;
	Button buttonClick;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.kamera_seite);

		vorschau = new Vorschau(this);
		((FrameLayout) findViewById(R.id.vorschau)).addView(vorschau);

		buttonClick = (Button) findViewById(R.id.ausloeser);
		buttonClick.setOnClickListener(new OnClickListener() {
			
		public void onClick(View v) {
				vorschau.camera.takePicture(shutterCallback, rawCallback,
						jpegCallback);
			}
		});

		Log.d(Constants.KAMERA_TAG, "onCreate'd");
	}

	ShutterCallback shutterCallback = new ShutterCallback() {
		public void onShutter() {
			Log.d(Constants.KAMERA_TAG, "onShutter'd");
		}
	};

	PictureCallback rawCallback = new PictureCallback() {
		public void onPictureTaken(byte[] data, Camera camera) {
			Log.d(Constants.KAMERA_TAG, "onPictureTaken - raw");
		}
	};

	PictureCallback jpegCallback = new PictureCallback() {
		public void onPictureTaken(byte[] data, Camera camera) {
			FileOutputStream outStream = null;
			try {
				String dirPath = (Environment.getExternalStorageDirectory().getPath()+"/kleinanzeigengenerator/");
				File pfad = new File(dirPath);
				pfad.mkdirs();
				File outputFile = new File (pfad, "Anzeigebild.jpg");
				outStream = new FileOutputStream(outputFile);
				outStream.write(data);
				outStream.close();
				Log.d(Constants.KAMERA_TAG, "onPictureTaken - wrote bytes: " + data.length);
			} catch (FileNotFoundException e) {
//				e.printStackTrace();
				finish();
			} catch (IOException e) {
//				e.printStackTrace();
				finish();
			} finally {
			}
			Log.d(Constants.KAMERA_TAG, "onPictureTaken - jpeg");
		}
	};

}
